package spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * Created by boot on 8/12/15.
 */

@Configuration
@ComponentScan
public class Application {
    @Bean
    MessageService mockService() {
        return new MessageService() {
            public String getMessage() {
                return "Hello world.";
            }
        };
    }

    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(Application.class);
        info(context);
        MessagePrinter printer = context.getBean(MessagePrinter.class);
        printer.printMsg();
    }


    private static void info(ApplicationContext context) {
        String[] beanNames = context.getBeanDefinitionNames();
        System.out.println("***** Loaded beans *********");
        for (String beanName : beanNames) {
            Object beanInstance = context.getBean(beanName);
            System.out.printf(">>>>> Bean name: %s. Class: %s\n", beanName, beanInstance.getClass().getName());
        }
        System.out.println("***** END Loaded beans *********");
    }
}
